package com.reddys.rad.inventory.controller;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import com.reddys.rad.inventory.db.ProductCategory;
import com.reddys.rad.inventory.utils.EntityManagerImpl;
import com.reddys.rad.inventory.utils.Utilities;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Logger;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.util.Callback;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;


/**
 * FXML Controller class
 *
 * @author Arun
 */
public class ProductCategoryController implements Initializable {

    Logger log = Utilities.getLogger("ProductCategoryController");
    @FXML
    private TextField categoryID;
    @FXML
    private TextField categoryName;
    @FXML
    private TextField status;
    @FXML
    private TextField remarks;
    @FXML
    private TableView productTable;
    @FXML
    private TableColumn tableProductID;
    @FXML
    private TableColumn tableProductName;
    @FXML
    private TableColumn tableItemCode;
    @FXML
    private TableColumn tableBatchCode;

    
    
    

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        try{
      populateProducttable();
           productTable.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent t) {
                ObservableList product=(ObservableList)
                productTable.getSelectionModel().getSelectedItem();
                categoryID.setText((String)product.get(0));
                categoryName.setText((String)product.get(1));
                status.setText((String)product.get(2));
                remarks.setText((String)product.get(3));
                
                
            }
        });
        }catch(Exception e){
             e.printStackTrace();
            log.info(e.getMessage());
        }
    }

    @FXML
    public void save(ActionEvent event) {
        try{
        System.out.println("product" + categoryID.getText());
        ProductCategory productCategory = new ProductCategory();
        //productCategory.setProductCategoryId(categoryID.getText());
        Boolean flag=false;
         if(categoryID.getText()!=null && categoryID.getText().length()>0){
          productCategory.setProductCategoryId(categoryID.getText());
          flag=true;
        }
        productCategory.setProductCategoryDescription(categoryName.getText());
        productCategory.setStatus(status.getText());
        productCategory.setRemarks(remarks.getText());
        
        
       /*EntityManager manager=getDBConnetion();
        manager.getTransaction().begin();
        manager.persist(product);
        manager.getTransaction().commit();*/
          ProductCategory productCategory1;
        if(flag){
            productCategory1=(ProductCategory) EntityManagerImpl.merge(productCategory);  
        }else {
            productCategory1=(ProductCategory) EntityManagerImpl.persist(productCategory);   
        }
         categoryID.setText(productCategory1.getProductCategoryId());
         populateProducttable();
        }catch(Exception e){
            e.printStackTrace();
            log.info(e.getMessage());
        }
    }

    @FXML
    public void clear(ActionEvent event) {
        categoryID.setText(null);
        categoryName.setText(null);
        status.setText(null);
        remarks.setText(null);
    }
    public static EntityManager getDBConnetion() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("purchasemodule");
        EntityManager manager = factory.createEntityManager();
        return manager;

    }
    
    public  void populateProducttable(){
        EntityManager manager=EntityManagerImpl.getEntityManager();
        Query query=manager.createNamedQuery("ProductCategory.findAll");
        List<ProductCategory> list= (List<ProductCategory>) query.getResultList();
           
           ObservableList list2= FXCollections.observableArrayList();
           if(list.size()>0){
            for(ProductCategory l:list){
                ObservableList list1= FXCollections.observableArrayList();
                list1.add(l.getProductCategoryId());
                list1.add(l.getProductCategoryDescription());
                list1.add(l.getStatus());
                list1.add(l.getRemarks());

               list2.add(list1);
            } 
           }
       
         productTable.getItems().clear();;
         productTable.setItems(list2);
         tableProductID.setMinWidth(250);
         tableProductID.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>(){

            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList<String>, String> cdf) {
              return new SimpleStringProperty( 
                      cdf.getValue().get(0).toString());
            }
           

           
        });
        tableProductName.setMinWidth(350);
        tableProductName.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>(){

            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList<String>, String> cdf) {
              return new SimpleStringProperty( cdf.getValue().get(1).toString());
            }
           

           
        });
        tableItemCode.setMinWidth(250);
        tableItemCode.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>(){

            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList<String>, String> cdf) {
              return new SimpleStringProperty( cdf.getValue().get(2).toString());
            }
           

           
        });
        tableBatchCode.setMinWidth(250);
        tableBatchCode.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>(){

            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList<String>, String> cdf) {
              return new SimpleStringProperty( cdf.getValue().get(3)!=null?cdf.getValue().get(3).toString():"");
            }
            
           

           
        });
        
        
       
        
    }
    
    
    
    
}
